package com.xxx.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.mall.constant.StatusEnum;
import com.xxx.mall.context.AuthUserContext;
import com.xxx.mall.converter.SpuConverter;
import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.dto.SpuDTO;
import com.xxx.mall.dto.SpuPageQuery;
import com.xxx.mall.exception.MallException;
import com.xxx.mall.mapper.SpuMapper;
import com.xxx.mall.model.Spu;
import com.xxx.mall.model.SpuAttrValue;
import com.xxx.mall.model.SpuDetail;
import com.xxx.mall.model.SpuExtension;
import com.xxx.mall.service.*;
import com.xxx.mall.vo.PageVO;
import com.xxx.mall.vo.SpuVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * spu信息 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-24
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Resource
    SpuConverter spuConverter;

    @Resource
    SpuAttrValueService spuAttrValueService;

    @Resource
    SpuDetailService spuDetailService;

    @Resource
    SpuExtensionService spuExtensionService;

    @Resource
    SkuService skuService;

    @Resource
    BrandService brandService;

    @Resource
    CategoryService categoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSpu(SpuDTO spuDTO) {
        Spu spu = spuConverter.toSpu(spuDTO);
        spu.setShopId(AuthUserContext.get().getTenantId());
        spu.setStatus(StatusEnum.DISABLE.value());
        spu.setPriceFee(spuDTO.getSkuList().get(0).getPriceFee());
        //保存spu
        baseMapper.insert(spu);
        //保存spu规格属性
        List<SpuAttrValue> spuAttrValue = spuConverter.toSpuAttrValue(spuDTO.getSpuAttrValues());
        spuAttrValue.forEach(attrValue -> attrValue.setSpuId(spu.getSpuId()));
        spuAttrValueService.saveBatch(spuAttrValue);
        //保存spu详情
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setDetail(spuDTO.getDetail());
        spuDetail.setSpuId(spu.getSpuId());
        spuDetailService.save(spuDetail);
        //保存spu扩展信息
        SpuExtension spuExtension = new SpuExtension();
        spuExtension.setSpuId(spu.getSpuId());
        spuExtension.setStock(spuDTO.getTotalStock());
        spuExtension.setActualStock(spuDTO.getTotalStock());
        spuExtensionService.save(spuExtension);
        //保存sku
        skuService.addSkus(spu.getSpuId(), spuDTO.getSkuList());
    }

    @Override
    public SpuVO queryBySpuId(Long id) {
        //查询spu基础信息
        SpuVO spuVO = baseMapper.queryBySpuId(id);
        //品牌信息
        spuVO.setBrand(brandService.queryById(spuVO.getBrandId()));
        //平台分类
        spuVO.setCategory(categoryService.queryById(spuVO.getCategoryId()));
        //店铺分类
        spuVO.setShopCategory(categoryService.queryById(spuVO.getShopCategoryId()));
        //sku信息
        spuVO.setSkus(skuService.queryListBySpuId(id));
        return spuVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpuInfo(SpuDTO spuDTO) {
        Spu spu = spuConverter.toSpu(spuDTO);
        //修改基础信息
        baseMapper.updateById(spu);
        //修改spu规格属性
        spuAttrValueService.updateSpuAttr(spu.getSpuId(), spuDTO.getSpuAttrValues());
        //修改spu详情
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setDetail(spuDTO.getDetail());
        spuDetail.setSpuId(spu.getSpuId());
        spuDetailService.updateBySpuId(spuDetail);
        //修改spu扩展信息
        SpuExtension spuExtension = new SpuExtension();
        spuExtension.setSpuId(spu.getSpuId());
        spuExtension.setStock(spuDTO.getTotalStock());
        spuExtension.setActualStock(spuDTO.getTotalStock());
        spuExtensionService.updateBySpuId(spuExtension);
        //修改sku
        skuService.updateSkus(spu.getSpuId(), spuDTO.getSkuList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBySpuId(Long id) {
        Spu spu = baseMapper.selectById(id);
        if (spu != null && !Objects.equals(spu.getStatus(), StatusEnum.DISABLE.value())) {
            throw new MallException("商品已上架，不能删除");
        }
        baseMapper.removeById(id);
        skuService.removeBySpuId(id);
    }

    @Override
    public PageVO<SpuVO> pageList(PageQuery pageQuery, SpuPageQuery spuPageQuery) {
        Page<SpuVO> page = PageDTO.of(pageQuery.getPage(), pageQuery.getSize());
        page = baseMapper.pageList(page, spuPageQuery);
        return PageVO.of(page);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        baseMapper.changeStatus(id, status);
    }
}
